package com.softeem.servlet;

import com.alibaba.druid.util.StringUtils;
import com.softeem.action.BaseServlet;
import com.softeem.entity.Course;
import com.softeem.entity.Student;
import com.softeem.service.StudentService;
import com.softeem.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.IOUtils;
import sun.util.PreHashedMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/StudentServlet/*")
@MultipartConfig
public class StudentServlet extends BaseServlet {

    private StudentService service = new StudentServiceImpl();


    public void findByCase() throws SQLException, IOException {
        String name = req.getParameter("sname");
        String male = req.getParameter("smale");
        //String pageNo = req.getParameter("pageNo");
//        String a = req.getParameter("aa");
//        System.out.println(a);
        Student stu = new Student();
        stu.setName(name);
        stu.setMale(male);
//
//        //pageNo等于空 赋初始值
//        if(StringUtils.isEmpty(pageNo)){
//            pageNo = "1";
//        }
//
//        Map map = service.findByCase(stu,pageNo);
//        map.put("stu",stu);
//        //将集合放入session
//        req.getSession().setAttribute("map",map);
//        //跳转到list.jsp
//        resp.sendRedirect("/list.jsp?pageNo="+pageNo);

        List<Student> list = service.findByCase(stu);

        req.getSession().setAttribute("list",list);
       // resp.sendRedirect("/list.jsp");
        resp.sendRedirect("/list.jsp");
        System.out.println(list);

    }

    public void findPages() throws SQLException, IOException {
        String pageNo = req.getParameter("pageNo");
        List<Student> list = service.findPages(pageNo);
        req.getSession().setAttribute("list",list);
        resp.sendRedirect("/list.jsp");

    }

    public void updateStu() throws InvocationTargetException, IllegalAccessException, SQLException, IOException {
        Student stu = new Student();
        Map<String,String[]> parameterMap = req.getParameterMap();
        BeanUtils.populate(stu,parameterMap);
        Integer i = service.updateStu(stu);
        req.getSession().setAttribute("list",findAll());
        resp.sendRedirect("/list.jsp");
    }

    public void delete() throws SQLException, IOException {
        Student stu = new Student();
        String stuid = req.getParameter("stuID");
        Integer i =  service.delete(stuid);
        if (i ==1){
            req.getSession().setAttribute("list",findAll());
            resp.sendRedirect("/list.jsp");
        }
    }

    public void findOne() throws Exception {
        String stuid = req.getParameter("stuID");
        Student stu = service.findOne(stuid);

        req.getSession().setAttribute("stu",stu);
        resp.sendRedirect("/updateStu.jsp");
    }

    public void addStu() throws SQLException, InvocationTargetException, IllegalAccessException, IOException {

        Student stu = new Student();
        Map<String, String[]> parameterMap =  req.getParameterMap();
        //System.out.println(parameterMap);
        BeanUtils.populate(stu,parameterMap);
        service.addStu(stu);
        resp.sendRedirect("/index2.jsp");
    }

//    public List<Student> findAll() throws SQLException {
//        //总页数
//        Student stu = new Student();
//        Integer total = service.total(stu);
//        req.getSession().setAttribute("total",total);
//        return service.findAll();
//
//    }
//
//    public void findPages() throws Exception {
//        String pagNo = req.getParameter("pageNo");
//        List<Student> list = service.findPages(pagNo);
//        System.out.println(list);
//        req.getSession().setAttribute("list",list);
//        resp.sendRedirect("/list.jsp");
//    }

        public List<Student> findAll() throws SQLException, IOException {
            Student stu = new Student();
        List<Student> list =service.findAll();
            Integer total =  service.total(stu);
            System.out.println(list);
        req.getSession().setAttribute("total",total);
        req.getSession().setAttribute("list",list);
        return service.findAll();

    }

    public void updateTea(){
        String name = req.getParameter("name");
        String psw = req.getParameter("password");
    }



    public void addCourse() throws Exception {
        Course cou = new Course();
        Map<String , String[]> parameterMap = req.getParameterMap();
        BeanUtils.populate(cou,parameterMap);
        service.addCourse(cou);
        req.getSession().setAttribute("list1",findCourse());
        resp.sendRedirect("/index3.jsp");
    }

    String FileName = "";

    public void upload() throws IOException, ServletException {
        String name = req.getParameter("name");
        Part part   = req.getPart("photo");

        //设置路径
        String realPath = req.getServletContext().getRealPath("/upload/");
        //创建文件夹
        File file = new File(realPath);
        //检查文件夹是否存在
        if(!file.exists()){
            file.mkdir();
        }

        //获取文件名称
        String fileName = part.getSubmittedFileName();

        FileName = "/upload/" + fileName;

        req.getSession().setAttribute("path","/upload/"+fileName);
        req.getSession().setAttribute("fileName",fileName);
        //上传
        part.write(realPath+fileName);
        System.out.println(1);
    }

    public void findCouByCno() throws SQLException, IOException {
        String cno = req.getParameter("cno");
        Course cou = service.findCouByCno(cno);

        req.getSession().setAttribute("cou",cou);
        resp.sendRedirect("/updateCou.jsp");
    }

    public void updateCou() throws InvocationTargetException, IllegalAccessException, SQLException, IOException, ServletException {
        Course cou = new Course();
        Map<String,String[]> parameterMap = req.getParameterMap();
        BeanUtils.populate(cou,parameterMap);

        Integer i = service.updateCou(cou);

        req.getSession().setAttribute("list1",findCourse());
        resp.sendRedirect("/course.jsp");

    }

    public List<Course> findCourse() throws SQLException, IOException, ServletException {
        Course course = new Course();

        List<Course> list1 = service.findCourse();

        System.out.println(list1);
        req.getSession().setAttribute("list1",list1);
        return service.findCourse();
    }

    public void save() throws Exception {
        Part part1 = req.getPart("picture1");

        //设置路径
        String realPath = req.getServletContext().getRealPath("/save/");
        //创建文件夹
        File file = new File(realPath);
        //检查文件夹是否存在
        if(!file.exists()){
            file.mkdir();
        }

        //获取文件名称
        String fileName1 = part1.getSubmittedFileName();


        req.getSession().setAttribute("path1","/save/"+fileName1);
        req.getSession().setAttribute("fileName",fileName1);

        //上传
        part1.write(realPath+fileName1);

        resp.sendRedirect("/teacher.jsp");
//        //获取上传的文件对象
//        Part part = req.getPart("picture");
//        //将文件以输入流的形式保存到数据库
//        List list = new ArrayList();
//        list.add(part.getSubmittedFileName());
//        list.add(part.getInputStream());
//
//        service.save(list);
    }

    public void login() throws Exception {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sign     = req.getParameter("sign");

        Integer i = service.login(username,password);
        //System.out.println(i);

        if (i == 1){
            req.getSession().setAttribute("username",username);
            req.getSession().setAttribute("password",password);
        }

        if (i == 1 && "on".equals(sign)){
            getCookie(username,password);
            req.getSession().setAttribute("list",findAll());
            req.getSession().setAttribute("list1",findCourse());
            resp.sendRedirect("/index.jsp");
        }else if (i == 1){
            req.getSession().setAttribute("list",findAll());
            req.getSession().setAttribute("list1",findCourse());
            resp.sendRedirect("/index.jsp");
        }else {
            resp.sendRedirect("/login.jsp?ss=0");
        }
    }

    private void getCookie(String username, String password) {
        //设置cookie
        Cookie c1 = new Cookie("username",username);
        Cookie c2 = new Cookie("password",password);

        //存活时间
        c1.setMaxAge(60*10);
        c2.setMaxAge(60*10);

        //访问路径
        c1.setPath("/");
        c2.setPath("/");

        //发送到cookie客户端
        resp.addCookie(c1);
        resp.addCookie(c2);
    }

    public void parseFile() throws IOException, ServletException, SQLException {
        //获取文件
        Part txtFile = req.getPart("txtFile");
        //获取上传的流对象
        InputStream in = txtFile.getInputStream();
        //逐行读取数据
        List<String> list = IOUtils.readLines(in,"gbk");
        //删除逗号

        for (String s : list){
            String[] split = s.split(",");
            service.addFile(split);
        }
        //释放资源
        in.close();
        txtFile.delete();

        findCourse();
        resp.sendRedirect("/index3.jsp");
    }

    public void back() throws IOException, SQLException {
        req.getSession().setAttribute("list",findAll());
        resp.sendRedirect("/list.jsp");
    }
}
